package htlgkr.nodlehs;

import java.util.ArrayList;
import java.util.Date;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TimePicker;
import android.widget.Toast;

public class CreateNewUnitActivity extends Activity {

	// private List<Teacher> personal;
	SQLiteDatabase db;
	NodlehsDatabase bridge = new NodlehsDatabase(this);
	TimePicker tp1;
	TimePicker tp2;
	Spinner tea;
	Spinner roo;
	Spinner sub;
	private static final int REQUESTCODE = 444;
	ArrayList<Teacher> teacherlist;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.newunit);
		Button b = (Button) findViewById(R.id.addSub);
		b.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				newUnitAdd();

			}
		});

		tp1 = (TimePicker) findViewById(R.id.timePicker1);
		tp2 = (TimePicker) findViewById(R.id.timePicker2);
		tp1.setIs24HourView(true);
		tp2.setIs24HourView(true);
		roo = (Spinner) findViewById(R.id.selectRoom);
		sub = (Spinner) findViewById(R.id.spinnerSub);
		tea = (Spinner) findViewById(R.id.spinnerteacher);
		teacherlist = new ArrayList<Teacher>();
		db = bridge.getWritableDatabase();

		loadRoo();
		loadTea();
		

		tea.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
			public void onItemSelected(AdapterView<?> adapterView, View view,
					int i, long l) {
				int pos = adapterView.getSelectedItemPosition();
				loadSub(teacherlist.get(pos));
			}

			public void onNothingSelected(AdapterView<?> adapterView) {
				return;
			}
		});

	}

	public void loadSub(Teacher t) {
		int id = t.getId();
		Cursor subjects = db.rawQuery(DBSubjectTbl.SQL_SUBSELTEACHER,
				new String[] { id + "" });
		String[] subjectArray = new String[subjects.getCount()];
		int idx = 0;
		while (subjects.moveToNext()) {
			subjectArray[idx] = subjects.getString(1);
			idx++;
		}
		ArrayAdapter<String> subjectAdapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_dropdown_item, subjectArray);
		sub.setAdapter(subjectAdapter);
	}

	private void loadRoo() {
		Cursor rooms = db.query("rooms", DBRoomsTbl.ALL_COLUMNS, "_id>?",
				new String[] { "0" }, null, null, null);
		String[] roomArray = new String[rooms.getCount()];
		int idx = 0;
		while (rooms.moveToNext()) {
			roomArray[idx] = rooms.getString(1);
			idx++;
		}
		ArrayAdapter<String> roomAdapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_dropdown_item, roomArray);
		roo.setAdapter(roomAdapter);
	}

	private void loadTea() {
		Cursor teacher = db.query("teachers", DBTeacherTbl.ALL_COLUMNS,
				"_id>?", new String[] { "0" }, null, null, null);
		String[] teacherArray = new String[teacher.getCount()];
		int idx = 0;
		while (teacher.moveToNext()) {
			teacherlist.add(new Teacher(Integer.parseInt(teacher.getString(0)),
					teacher.getString(1), teacher.getString(2)));
			teacherArray[idx] = teacher.getString(1) + " "
					+ teacher.getString(2);
			idx++;
		}
		ArrayAdapter<String> roomAdapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_dropdown_item, teacherArray);
		tea.setAdapter(roomAdapter);
	}

	private void newUnitAdd() {
		Intent i = new Intent();
		setResult(333, i);
		finish();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.layout.unitmenu, menu);
		return true;
	}
	
	public void addUnit(){
		int bhour = tp1.getCurrentHour();
		int bmin = tp1.getCurrentMinute();
		String begin = bhour +":" +bmin;
		
		int ehour = tp2.getCurrentHour();
		int emin = tp2.getCurrentMinute();
		String end = ehour +":"+emin;
		
		Color c = new Color();
		
		
		SQLiteStatement stmt = db.compileStatement(DBUnitTbl.SQL_INSERT);
		db.beginTransaction();
		long id = 0;
		try {
			stmt.bindString(1, begin);
			stmt.bindString(2, end);
			//stmt.bindString(3, desc);
			id = stmt.executeInsert();
			db.setTransactionSuccessful();
			
		} catch (Exception e) {
			Log.e("DBInsert Subject", e.getMessage().toString());
		} finally{
			db.endTransaction();
			db.close();
		}
	}
	
	public void openColChoose(final View v){
		Intent i = new Intent(this, ColorChooser.class);
		startActivityForResult(i, REQUESTCODE);
	}
	
	@Override
	protected void onActivityResult(int requestCode,int resultCode, Intent data){
		if(resultCode == Activity.RESULT_OK){
			Bundle extras = data.getExtras();
			int cs = extras.getInt("COLOR");
			Toast.makeText(getApplicationContext(), cs+"", Toast.LENGTH_LONG).show();
		}
		else Toast.makeText(getApplicationContext(), "no color selected", Toast.LENGTH_LONG).show();
	}

}
